﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Web;

using Factonomy.AzureStorageUtility.WindowsApp.Azure;
using Factonomy.AzureStorageUtility.WindowsApp.Azure.Storage;
using Factonomy.AzureStorageUtility.WindowsApp.Resources;

namespace Factonomy.AzureStorageUtility.WindowsApp.UI.Forms
{
    public partial class TextViewer : Form
    {
        private string _accountName = string.Empty;
        private string _containerName = string.Empty;
        private string _blobName = string.Empty;
        private bool _textChanged = false;
        private string _originalText = "Factonomy Windows Azure Storage Utility - Text Editor";

        public TextViewer(string accountName, string containername, string blobName)
        {
            InitializeComponent();

            // set the window icon
            this.Icon = ResourceManager.GetAppIcon();

            try
            {
                this._accountName = accountName;
                this._containerName = containername;
                this._blobName = blobName;

                this.uiCloseToolStripMenuItem.Click += new EventHandler(uiCloseToolStripMenuItem_Click);
                this.uiSaveToolStripMenuItem.Click += new EventHandler(uiSaveToolStripMenuItem_Click);
                this.uiTextbox.TextChanged += new EventHandler(uiTextbox_TextChanged);

                this.Text = this._originalText;

                using (BlobManager blob = new BlobManager())
                {
                    blob.Load(this._accountName);
                    MemoryStream stream = blob.GetBlobContents(this._containerName, this._blobName);
                    this.uiTextbox.Text = new StreamReader(stream).ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            this.PromptForSave();
            base.OnClosing(e);
        }

        void uiTextbox_TextChanged(object sender, EventArgs e)
        {
            if (!this._textChanged)
            {
                this.Text = this._originalText + "*";
                this._textChanged = true;
            }
        }

        private void SaveBlob()
        {
            try
            {
                using (BlobManager blob = new BlobManager())
                {
                    blob.Load(this._accountName);
                    blob.SaveBlob(this._containerName, this._blobName, this.uiTextbox.Text);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
        }

        void uiSaveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.SaveBlob();
            this.Text = this._originalText;
            this._textChanged = false;
        }

        private void PromptForSave()
        {
            if (this._textChanged)
            {
                if (MessageBox.Show("Do you wish to save your changes?", "Confirm Save", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    this.SaveBlob();
                }
            }
            
        }

        void uiCloseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.PromptForSave();
            this.Close();
        }
    }
}
